/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2023-02-12
 *
 * 数据表格钩子
 */
import { useBase } from "@/hooks/base/useBase"

export const useTable = (model, dataForm) => {

  const { i18n } = useBase()

  const refresh = inject("refresh")

  const formRef = ref()

  const dataList = ref([])
  const pageIndex = ref(1)
  const pageSize = ref(10)
  const totalPage = ref(0)
  const dataListLoading = ref(false)
  const dataListSelections = ref([])


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置每页显示条数
   *
   * @param {[type]} size 显示条数
   * @return {[type]}
   */
  const sizeChangeHandle = (size) => {
    pageSize.value = size
    pageIndex.value = 1
    getDataList()
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置当前页数
   *
   * @param {[type]} size 当前页数
   * @return {[type]}
   */
  const currentChangeHandle = (size) => {
    pageIndex.value = size
    getDataList()
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 多选函数
   *
   * @param {[type]} val 行号
   * @return {[type]}
   */
  const selectionChangeHandle = (val) => {
    dataListSelections.value = val
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 获取数据列表
   *
   * @param {[type]} is_search 是否为搜索操作
   * @return {[type]}
   */
  const getDataList = (is_search) => {
    is_search = is_search || false

    if (is_search) {
      pageIndex.value = 1
    }

    dataListLoading.value = true

    $http({
      url: $http.adornUrl('/' + model + '/list'),
      method: 'get',
      params: $http.adornParams({
        is_search: is_search.value,
        page: pageIndex.value,
        limit: pageSize.value,
        name: dataForm.name,
        head: dataForm.head,
        type: dataForm.type,
        title: dataForm.title,
        mobile: dataForm.mobile,
        username: dataForm.username,
        nickname: dataForm.nickname,
        realname: dataForm.realname,
        id_card_no: dataForm.id_card_no,
        content: dataForm.content,
        category: dataForm.category,

        member_role_id: dataForm.member_role_id,
        member_username: dataForm.member_username,
        member_nickname: dataForm.member_nickname,

        squad_no: dataForm.squad_no,
        squad_squad_no: dataForm.squad_squad_no,
        squad_title: dataForm.squad_title,

        id: dataForm.id,
        pid: dataForm.pid,
        role_id: dataForm.role_id,
        unit_id: dataForm.unit_id,
        parent_id: dataForm.parent_id,
        member_id: dataForm.member_id,
        category_id: dataForm.category_id,
        location_id: dataForm.location_id,
        dictionary_id: dataForm.dictionary_id,
        organization_id: dataForm.organization_id,
        course_id: dataForm.course_id,
        unit_id: dataForm.unit_id,
        point_id: dataForm.point_id,
        intensify_id: dataForm.intensify_id,
        paper_id: dataForm.paper_id,
        squad_id: dataForm.squad_id,


        audit_status: dataForm.audit_status,
        order_status: dataForm.order_status,
        certification_status: dataForm.certification_status,
        is_hidden: dataForm.is_hidden,
        is_recommend: dataForm.is_recommend,

        add_time: dataForm.add_time,
        valid_time: dataForm.valid_time,
        create_time: dataForm.create_time,
        last_login_time: dataForm.last_login_time,

        archive_contact: dataForm.contact,
        archive_contact_mobile: dataForm.contact_mobile,

        member_username: dataForm.member_username,
        member_nickname: dataForm.member_nickname,

        customer_title: dataForm.customer_title,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataList.value = data.data.data
        totalPage.value = data.data.total
      } else {
        dataList.value.length = 0
        totalPage.value = 0
        $message.error(data.message)
      }
      dataListLoading.value = false
    })
  }

  // 改变状态
  const handleStatus = ($event, id, field) => {
    $http({
      url: $http.adornUrl('/' + model + '/status'),
      method: 'post',
      data: {
        id: id,
        field: field,
        value: $event
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        $message({
          message: i18n('common.handle_success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            getDataList()
          },
        })
      } else {
        $message.error(i18n(data.message))
      }
    })
  }


  // 导出
  const handleExport = () => {
    $http({
      url: $http.adornUrl('/' + model + '/export'),
      method: 'post',
      data: $http.adornData({
        id: dataForm.id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        window.open(data.data)
      } else {
        $message.error(i18n(data.message))
      }
    })
  }


  // 删除
  const deleteHandle = (id) => {
    var ids = id ? [id] : dataListSelections.map((item) => {return item.id})

    let message = i18n('common.to_determine_the') +
                  i18n('common.current_check') +
                  i18n('common.for') +
                  `${id ? i18n('common.delete') : i18n('common.batch_delete')}` +
                  i18n('common.handle') + `?`

    $messagebox.confirm(message, i18n('common.prompt'), {
      confirmButtonText: i18n('common.confirm'),
      cancelButtonText: i18n('common.cancel'),
      type: 'warning',
    }).then(() => {
      $http({
        url: $http.adornUrl('/' + model + '/delete'),
        method: 'post',
        data: { id: $http.adornData(ids, false) },
      }).then(({ data }) => {
        if (data && data.status === 200) {
          $message({
            message: i18n('common.handle_success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              // getDataList()
              refresh()
            },
          })
        } else {
          $message.error(i18n(data.message))
        }
      })
    }).catch(() => {})
  }

  return {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    dataListSelections,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  }
}
